package io.framework.modules.soil.controller;

import io.framework.common.utils.DateUtils;
import io.framework.common.utils.Result;
import io.framework.modules.soil.enums.SoilTypeEnum;
import io.framework.modules.soil.service.SoilService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.*;

/**
 * 土壤环境质量监测
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-25
 */
@RestController
@Api(tags="土壤环境质量监测")
public class SoilDataController {
    @Autowired
    private SoilService soilService;

    @GetMapping("soil/info/pass")
    @ApiOperation("土壤达标情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "居民区周边土壤 基本农田 畜牧养殖周边土壤 园地 饮用水源地 生活垃圾场周边土壤", paramType = "query", dataType = "int",required = true),
            @ApiImplicitParam(name = "date", value = "时间 2019", paramType = "query", dataType = "String",required = true),
    })
    public Result<Map<String,Object>> pass(@ApiIgnore @RequestParam Map<String, Object> params) {
        params.put("name",params.get("type").toString().replaceAll(" ",""));
        if(params.containsKey("date")){
            String date =params.get("date").toString();
            if(StringUtils.isBlank(date)){
                //params.put("date",DateUtils.format(DateUtils.addDateYears(new Date(),-1),DateUtils.DATE_YEAR_PATTERN));
                String a = soilService.getDate();
                params.put("date",a.substring(0,4));
            }
        }
        List<Map<String, Object>> list = soilService.getPassInfo(params);
        List<Map<String, Object>> passNum = soilService.getPassNum(params);
        Map<String,Object> map = new HashMap<>(2);
        map.put("villageList",list);
        map.put("passNum",passNum);
        return new Result<Map<String,Object>>().ok(map);
    }

    @GetMapping("soil/info/monitorPoint")
    @ApiOperation("土壤监测点位情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间 2019", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> monitorPoint(@ApiIgnore @RequestParam Map<String, Object> params) {
        if(params.containsKey("date")){
            String date =params.get("date").toString();
            if(StringUtils.isBlank(date)){
               // params.put("date",DateUtils.format(DateUtils.addDateYears(new Date(),-1),DateUtils.DATE_YEAR_PATTERN));
                String a = soilService.getDate();
                params.put("date",a.substring(0,4));
            }
        }
        List<Map<String,Object>> mapList =soilService.getMonitorPointInfo(params);

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("soil/info/historyQuality")
    @ApiOperation("历史土壤环境质量情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间 2019", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "villageName", value = "村庄名称  例：吉阳区三罗村", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> emQuality(@ApiIgnore @RequestParam Map<String, Object> params) {
        if(params.containsKey("date")){
            String date =params.get("date").toString();
            if(StringUtils.isBlank(date)){
                //params.put("date",DateUtils.format(DateUtils.addDateYears(new Date(),-1),DateUtils.DATE_YEAR_PATTERN));
                String a = soilService.getDate();
                params.put("date",a.substring(0,4));
            }
        }
        List<Map<String,Object>> mapList =soilService.getHistoryQuality(params);
        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("soil/info/getVillageNameList")
    @ApiOperation("获取所有村庄名称")
    public Result<List<Map<String,Object>>> getVillageNameList(){

        List<Map<String,Object>> mapList =soilService.getVillageNameList();

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("soil/info/getPointNamaList")
    @ApiOperation("根据村庄获取当前村庄所有的土地利用类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "villageName", value = "村庄名称  例：吉阳区三罗村", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> getPointNamaList(@ApiIgnore @RequestParam Map<String, Object> params){

        List<Map<String,Object>> mapList =soilService.getPointNamaList(params);

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

}